<!DOCTYPE html>
<html>
<head>
<title>quzrtz定时器.md</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">

<style>
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

body {
	font-family: "Segoe WPC", "Segoe UI", "SFUIText-Light", "HelveticaNeue-Light", sans-serif, "Droid Sans Fallback";
	font-size: 14px;
	padding: 0 12px;
	line-height: 22px;
	word-wrap: break-word;
}

#code-csp-warning {
	position: fixed;
	top: 0;
	right: 0;
	color: white;
	margin: 16px;
	text-align: center;
	font-size: 12px;
	font-family: sans-serif;
	background-color:#444444;
	cursor: pointer;
	padding: 6px;
	box-shadow: 1px 1px 1px rgba(0,0,0,.25);
}

#code-csp-warning:hover {
	text-decoration: none;
	background-color:#007acc;
	box-shadow: 2px 2px 2px rgba(0,0,0,.25);
}


body.scrollBeyondLastLine {
	margin-bottom: calc(100vh - 22px);
}

body.showEditorSelection .code-line {
	position: relative;
}

body.showEditorSelection .code-active-line:before,
body.showEditorSelection .code-line:hover:before {
	content: "";
	display: block;
	position: absolute;
	top: 0;
	left: -12px;
	height: 100%;
}

body.showEditorSelection li.code-active-line:before,
body.showEditorSelection li.code-line:hover:before {
	left: -30px;
}

.vscode-light.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(0, 0, 0, 0.15);
}

.vscode-light.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(0, 0, 0, 0.40);
}

.vscode-dark.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(255, 255, 255, 0.4);
}

.vscode-dark.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(255, 255, 255, 0.60);
}

.vscode-high-contrast.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(255, 160, 0, 0.7);
}

.vscode-high-contrast.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(255, 160, 0, 1);
}

img {
	max-width: 100%;
	max-height: 100%;
}

a {
	color: #4080D0;
	text-decoration: none;
}

a:focus,
input:focus,
select:focus,
textarea:focus {
	outline: 1px solid -webkit-focus-ring-color;
	outline-offset: -1px;
}

hr {
	border: 0;
	height: 2px;
	border-bottom: 2px solid;
}

h1 {
	padding-bottom: 0.3em;
	line-height: 1.2;
	border-bottom-width: 1px;
	border-bottom-style: solid;
}

h1, h2, h3 {
	font-weight: normal;
}

h1 code,
h2 code,
h3 code,
h4 code,
h5 code,
h6 code {
	font-size: inherit;
	line-height: auto;
}

a:hover {
	color: #4080D0;
	text-decoration: underline;
}

table {
	border-collapse: collapse;
}

table > thead > tr > th {
	text-align: left;
	border-bottom: 1px solid;
}

table > thead > tr > th,
table > thead > tr > td,
table > tbody > tr > th,
table > tbody > tr > td {
	padding: 5px 10px;
}

table > tbody > tr + tr > td {
	border-top: 1px solid;
}

blockquote {
	margin: 0 7px 0 5px;
	padding: 0 16px 0 10px;
	border-left: 5px solid;
}

code {
	font-family: Menlo, Monaco, Consolas, "Droid Sans Mono", "Courier New", monospace, "Droid Sans Fallback";
	font-size: 14px;
	line-height: 19px;
}

body.wordWrap pre {
	white-space: pre-wrap;
}

.mac code {
	font-size: 12px;
	line-height: 18px;
}

pre:not(.hljs),
pre.hljs code > div {
	padding: 16px;
	border-radius: 3px;
	overflow: auto;
}

/** Theming */

.vscode-light,
.vscode-light pre code {
	color: rgb(30, 30, 30);
}

.vscode-dark,
.vscode-dark pre code {
	color: #DDD;
}

.vscode-high-contrast,
.vscode-high-contrast pre code {
	color: white;
}

.vscode-light code {
	color: #A31515;
}

.vscode-dark code {
	color: #D7BA7D;
}

.vscode-light pre:not(.hljs),
.vscode-light code > div {
	background-color: rgba(220, 220, 220, 0.4);
}

.vscode-dark pre:not(.hljs),
.vscode-dark code > div {
	background-color: rgba(10, 10, 10, 0.4);
}

.vscode-high-contrast pre:not(.hljs),
.vscode-high-contrast code > div {
	background-color: rgb(0, 0, 0);
}

.vscode-high-contrast h1 {
	border-color: rgb(0, 0, 0);
}

.vscode-light table > thead > tr > th {
	border-color: rgba(0, 0, 0, 0.69);
}

.vscode-dark table > thead > tr > th {
	border-color: rgba(255, 255, 255, 0.69);
}

.vscode-light h1,
.vscode-light hr,
.vscode-light table > tbody > tr + tr > td {
	border-color: rgba(0, 0, 0, 0.18);
}

.vscode-dark h1,
.vscode-dark hr,
.vscode-dark table > tbody > tr + tr > td {
	border-color: rgba(255, 255, 255, 0.18);
}

.vscode-light blockquote,
.vscode-dark blockquote {
	background: rgba(127, 127, 127, 0.1);
	border-color: rgba(0, 122, 204, 0.5);
}

.vscode-high-contrast blockquote {
	background: transparent;
	border-color: #fff;
}
</style>

<style>
/* Tomorrow Theme */
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
/* Original theme - https://github.com/chriskempson/tomorrow-theme */

/* Tomorrow Comment */
.hljs-comment,
.hljs-quote {
	color: #8e908c;
}

/* Tomorrow Red */
.hljs-variable,
.hljs-template-variable,
.hljs-tag,
.hljs-name,
.hljs-selector-id,
.hljs-selector-class,
.hljs-regexp,
.hljs-deletion {
	color: #c82829;
}

/* Tomorrow Orange */
.hljs-number,
.hljs-built_in,
.hljs-builtin-name,
.hljs-literal,
.hljs-type,
.hljs-params,
.hljs-meta,
.hljs-link {
	color: #f5871f;
}

/* Tomorrow Yellow */
.hljs-attribute {
	color: #eab700;
}

/* Tomorrow Green */
.hljs-string,
.hljs-symbol,
.hljs-bullet,
.hljs-addition {
	color: #718c00;
}

/* Tomorrow Blue */
.hljs-title,
.hljs-section {
	color: #4271ae;
}

/* Tomorrow Purple */
.hljs-keyword,
.hljs-selector-tag {
	color: #8959a8;
}

.hljs {
	display: block;
	overflow-x: auto;
	color: #4d4d4c;
	padding: 0.5em;
}

.hljs-emphasis {
	font-style: italic;
}

.hljs-strong {
	font-weight: bold;
}
</style>

<style>
/*
 * Markdown PDF CSS
 */

 body {
	font-family:  "Meiryo", "Segoe WPC", "Segoe UI", "SFUIText-Light", "HelveticaNeue-Light", sans-serif, "Droid Sans Fallback";
}

pre {
	background-color: #f8f8f8;
	border: 1px solid #cccccc;
	border-radius: 3px;
	overflow-x: auto;
	white-space: pre-wrap;
	overflow-wrap: break-word;
}

pre:not(.hljs) {
	padding: 23px;
	line-height: 19px;
}

blockquote {
	background: rgba(127, 127, 127, 0.1);
	border-color: rgba(0, 122, 204, 0.5);
}

.emoji {
	height: 1.4em;
}

/* for inline code */
:not(pre):not(.hljs) > code {
	color: #C9AE75; /* Change the old color so it seems less like an error */
	font-size: inherit;
}

/* Page Break : use <div class="page"/> to insert page break
-------------------------------------------------------- */
.page {
	page-break-after: always;
}

</style>

</head>
<body>
<h2 id="%E4%BE%9D%E8%B5%96">依赖</h2>
<pre class="hljs"><code><div>   <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.quartz-scheduler<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>quartz<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.2.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.quartz-scheduler<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>quartz-jobs<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.2.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- https://mvnrepository.com/artifact/org.apache.archiva.redback.components/spring-quartz --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.apache.archiva.redback.components<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-quartz<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
</div></code></pre>
<h2 id="%E9%85%8D%E7%BD%AE">配置</h2>
<ul>
<li>一般XML写死的配置spring-quartz-simple.xml</li>
</ul>
<pre class="hljs"><code><div><span class="hljs-meta">&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
        "http://www.springframework.org/dtd/spring-beans-2.0.dtd"&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">beans</span>&gt;</span>
	<span class="hljs-comment">&lt;!-- 要调用的工作类 --&gt;</span>
	<span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"jobTask"</span>
		<span class="hljs-attr">class</span>=<span class="hljs-string">"com.hd.job.JobTask"</span>&gt;</span>
	<span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>
	<span class="hljs-comment">&lt;!-- 定义调用对象和调用对象的方法 --&gt;</span>
	 <span class="hljs-comment">&lt;!-- MethodInvokingJobDetailFactoryBean不支持序列化 --&gt;</span>  
   <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"jobTaskDetail"</span>
		<span class="hljs-attr">class</span>=<span class="hljs-string">"org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"</span>&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"targetObject"</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">"jobTask"</span> /&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"targetMethod"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"task"</span> /&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"concurrent"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"false"</span> /&gt;</span>
	<span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>
	<span class="hljs-comment">&lt;!-- 定义触发时间 CronTriggerBean 框架 --&gt;</span>
	<span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"jobTaskTrigger"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"org.springframework.scheduling.quartz.CronTriggerFactoryBean"</span>&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"jobDetail"</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">"jobTaskDetail"</span> /&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"cronExpression"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"0/5 * * * * ?"</span> /&gt;</span>
	<span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>

	<span class="hljs-comment">&lt;!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 --&gt;</span>
	<span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"schedulerFactory"</span>  <span class="hljs-attr">lazy-init</span>=<span class="hljs-string">'false'</span>
		<span class="hljs-attr">class</span>=<span class="hljs-string">"org.springframework.scheduling.quartz.SchedulerFactoryBean"</span>&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"triggers"</span>&gt;</span>
			<span class="hljs-tag">&lt;<span class="hljs-name">list</span>&gt;</span>
				<span class="hljs-tag">&lt;<span class="hljs-name">ref</span> <span class="hljs-attr">bean</span>=<span class="hljs-string">"jobTaskTrigger"</span> /&gt;</span>
			<span class="hljs-tag">&lt;/<span class="hljs-name">list</span>&gt;</span>
		<span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span>
	<span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">beans</span>&gt;</span>
</div></code></pre>
<ul>
<li>将定时器持久化到数据库中的定时器配置</li>
</ul>
<pre class="hljs"><code><div><span class="hljs-meta">&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
        "http://www.springframework.org/dtd/spring-beans-2.0.dtd"&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">beans</span>&gt;</span>
   <span class="hljs-comment">&lt;!--quartz持久化定时  --&gt;</span>
	<span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"schedulerFactoryBean"</span>  <span class="hljs-attr">lazy-init</span>=<span class="hljs-string">"true"</span>
		<span class="hljs-attr">class</span>=<span class="hljs-string">"org.springframework.scheduling.quartz.SchedulerFactoryBean"</span>&gt;</span>
		<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"dataSource"</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">"dataSource"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span>    
        <span class="hljs-comment">&lt;!--可选，QuartzScheduler 启动时更新己存在的Job，这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了 --&gt;</span>      
        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"overwriteExistingJobs"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"true"</span> /&gt;</span>      
         <span class="hljs-comment">&lt;!--必须的，QuartzScheduler 延时启动，应用启动完后 QuartzScheduler 再启动 --&gt;</span>    
        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"startupDelay"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"30"</span> /&gt;</span>    
        <span class="hljs-comment">&lt;!-- 设置自动启动 --&gt;</span>    
        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"autoStartup"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"true"</span> /&gt;</span>  
        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"applicationContextSchedulerContextKey"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"applicationContextKey"</span> /&gt;</span>    
        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"configLocation"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"classpath:spring/spring-quartz.properties"</span> /&gt;</span>    
	<span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">beans</span>&gt;</span>
</div></code></pre>
<p>spring-quartz.properties内容</p>
<pre class="hljs"><code><div>#==============================================================    
#Configure Main Scheduler Properties    
#==============================================================     
org.quartz.scheduler.instanceName = defaultScheduler  
org.quartz.scheduler.instanceId = AUTO  
  
#==============================================================    
#Configure JobStore    
#==============================================================   
orgorg.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX  
orgorg.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate  
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true  
org.quartz.jobStore.clusterCheckinInterval = 20000    
org.quartz.jobStore.dataSource = myDS  
org.quartz.jobStore.maxMisfiresToHandleAtATime = 1  
org.quartz.jobStore.misfireThreshold = 120000  
org.quartz.jobStore.txIsolationLevelSerializable = true  
   
#==============================================================    
#Configure ThreadPool    
#==============================================================   
orgorg.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool  
org.quartz.threadPool.threadCount = 10  
org.quartz.threadPool.threadPriority = 5  
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true  
  
#==============================================================  
#Skip Check Update  
#update:true  
#not update:false  
#==============================================================  
org.quartz.scheduler.skipUpdateCheck = true   
  
#============================================================================     
# Configure Plugins      
#============================================================================        
#orgorg.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin     
#orgorg.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin  
#org.quartz.plugin.shutdownhook.cleanShutdown = true  
</div></code></pre>
<pre><code>注意：spring-quzrtz-simple.xml和spring-quzrtz.xml只能使用其中一个，选择持久化定时器的请选择spring-quzrtz.xml，下面的配置都是与spring-quzrtz.xml相关
</code></pre>
<h3 id="%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%B8%E5%85%B3%E7%9A%84sql">数据库相关的sql</h3>
<pre><code>src/main/resources/db/**,quzrtz.sql对应的是mysql版本的，quartz-oracle.sql对应得是oracle的
</code></pre>
<h2 id="%E5%85%B6%E4%BB%96java%E4%BB%A3%E7%A0%81">其他Java代码</h2>
<h3 id="%E5%AE%9E%E4%BD%93">实体</h3>
<pre><code>src/main/java/com/hd/entity/job/QuartzScheduleJob.java,自定义保存定时相关的参数，方便在页面上展示查看
</code></pre>
<h3 id="service">service</h3>
<pre><code>src/main/java/com/hd/service/job/QuartzScheduleJobService.java,src/main/java/com/hd/service/job/QuartzScheduleJobServiceImpl.java,实现定时器的添加、删除、更新
</code></pre>
<h2 id="%E8%B0%83%E7%94%A8">调用</h2>
<pre><code>使用工厂模式、反射调用已经持久化到数据库中的定时器，src/main/java/com/hd/job/QuartzJobFactory.java
</code></pre>
<h2 id="%E5%AE%9A%E6%97%B6%E5%99%A8%E7%AE%A1%E7%90%86%E9%A1%B5%E9%9D%A2%E5%8F%AF%E8%A7%86%E5%8C%96%E7%AE%A1%E7%90%86">定时器管理(页面可视化管理)</h2>
<pre><code>src/main/java/com/hd/controller/job/QuartzScheduleJobController.java
</code></pre>
<h3 id="%E5%8F%AF%E8%A7%86%E5%8C%96%E5%AF%B9%E5%BA%94%E7%9A%84jsp%E9%A1%B5%E9%9D%A2">可视化对应的jsp页面</h3>
<pre><code>src/main/webapp/jsp/quartzScheduleJob/**
</code></pre>
<h2 id="%E6%B7%BB%E5%8A%A0%E5%AE%9A%E6%97%B6%E5%99%A8%E9%9C%80%E8%A6%81%E6%B3%A8%E6%84%8F%E7%9A%84%E5%9C%B0%E6%96%B9">添加定时器需要注意的地方</h2>
<ul>
<li>直接使用指定的时间或cron表达式，如 2019-06-21 10:30:30或0/5 * * * * ?</li>
<li>任务名称、任务组、任务触发器、任务触发器组请使用英文，任务名称和触发器名称请是唯一的</li>
<li>bean是存在spring容器中的bean,比如UserService对应的bean就是userService,已在service层中使用@Service注解注释过</li>
<li>执行方法，bean中需要调用的方法名</li>
<li>参数
执行参数的写法为&quot;参数名:类型:值&quot;,如&quot;id:Integer:1&quot;，注意不包括双引号，多个参数用逗号隔开。 参数类型为java类型，主要提供Integer、Long、String、Date、TimeStamp、float、double类型，cron表达式和执行时间需要填写其中一个, 編輯主要是修改cron表达式或执行时间
<img src="file:///d:/workspace/eclipse_workspace/work1/ssm_maven/doc/img/2019-06-21_095723.png" alt=""></li>
</ul>

</body>
</html>
